using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text.pdf;
using BlueFinity.mvNET.CoreObjects;
using mvLibrary.Helpers.Utils;
using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;

namespace mvLibrary.Helpers.Generators
{
    public class PdfGenerator
    {
        private static PdfGenerator instance;
        private static int REPORT_NUMBER = 0;
        private const int MAX_REPORT_NUMBER = 2;
        private const string REPORT_NAME_FORMAT = "report{0}_{1}.pdf";
        private PdfGenerator() { }

        public static PdfGenerator Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PdfGenerator();
                }
                return instance;
            }
        }

        public string GeneratePdfReport(string innerPDF, float leftMargin, float rightMargin, float topMargin, float bottonMargin, string footing, string formName = "")
        {
            string filename = this.GetCompleteFileName(REPORT_NUMBER, formName);

            if (File.Exists(filename))
            {
                REPORT_NUMBER = (REPORT_NUMBER <= MAX_REPORT_NUMBER ? ++REPORT_NUMBER : 0);
                filename = this.GetCompleteFileName(REPORT_NUMBER, formName);
                try
                {
                    //Borramos el siguiente
                    File.Delete(this.GetCompleteFileName(REPORT_NUMBER + 1, formName));
                }
                catch
                {
                    MessageBox.Show(MessageUtils.GetCannotDeleteReportMessage(filename), "Error al eliminar reporte", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return string.Empty;
                }

            }

            using (FileStream fStream = new FileStream(filename, FileMode.OpenOrCreate))
            {
                Document document = new Document(iTextSharp.text.PageSize.A4, leftMargin, rightMargin, topMargin, bottonMargin);
                try
                {
                    PdfWriter pdfw = PdfWriter.GetInstance(document, fStream);
                    // PdfSharp.Pdf.PdfDocument pdfDocument = new PdfSharp.Pdf.PdfDocument();


                    string firstline = DataBASIC.Field(innerPDF, DataBASIC.AM, 1);
                    if (firstline.StartsWith(".header"))
                    {
                        HeaderFooter header = new HeaderFooter(new Phrase(firstline.Substring(8, firstline.Length - 8)), false);
                        header.Border = Rectangle.BOTTOM_BORDER;
                        document.Header = header;
                    }
                    document.Open();
                    BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.MACROMAN, false, false, null, null);
                    float fontSize = 8;
                    // Graphics horizontalLine;


                    int pageNumber = 1;

                    this.PrintIfItsReciboDeSueldo(document, formName, pageNumber);
                    for (int indice = 1; indice <= DataBASIC.DCount(innerPDF, DataBASIC.AM); ++indice)
                    {
                        string textLine = DataBASIC.Field(innerPDF, DataBASIC.AM, indice);

                        if (textLine.StartsWith(".newpage"))
                        {
                            document.NewPage();
                            pageNumber++;
                            this.PrintIfItsReciboDeSueldo(document, formName,pageNumber);
                            continue;
                        }
                        if (textLine.StartsWith(".small"))
                        {
                            fontSize = 5; // equivale a 132 columnas
                            continue;
                        }
                        if (textLine.StartsWith(".half"))
                        {
                            fontSize = 6; // equivale a 100 columnas
                            continue;
                        }
                        if (textLine.StartsWith(".big"))
                        {
                            fontSize = 8; // equivale a 80 columnas
                            continue;
                        }
                        if (textLine.StartsWith(".header"))
                        {
                            continue;
                        }
                        if (textLine.Length == 0)
                        {
                            textLine = " ";
                        }
                        document.Add(new Paragraph(textLine, new iTextSharp.text.Font(bf, fontSize)));
                    }
                    document.Close();
                }
                catch (DocumentException doEx)
                {
                    MessageBox.Show(doEx.Message);
                    return string.Empty;
                }
                catch (IOException ioEX)
                {
                    MessageBox.Show(ioEX.Message);
                    return string.Empty;
                }
                fStream.Close();
            }

            return filename;
        }

        private void PrintIfItsReciboDeSueldo(Document document, string formName,int pageNumber)
        {
            if (formName.Equals("ReciboSueldos"))
            {
                var reciboName = pageNumber % 2 == 0 ? "recibo_template_empleador.jpeg" : "recibo_template.jpeg";
                Jpeg foto = new Jpeg(string.Format(@"\\90.0.0.90\Deploy\{0}", reciboName));
                foto.Alignment = Image.UNDERLYING;

                foto.ScaleToFit(600, 679);
                foto.SetAbsolutePosition(15, 125);
                document.Add(foto);
            }

        }

        private string GetCompleteFileName(int reportNumber, string formName)
        {
            string reportName = string.Format(REPORT_NAME_FORMAT, reportNumber, formName);
            string filename = !System.Environment.MachineName.Equals("SANTINOTEBOOK") ? "C:\\" + reportName : "D:\\Fenk\\fenk\\" + reportName;

            return filename;
        }
    }
}
